package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.common.result.TreeResult;
import com.jy.wms.pojo.WmsLibraryArea;
import com.jy.wms.pojo.WmsLibraryPosition;
import com.jy.wms.pojo.WmsOwner;
import com.jy.wms.pojo.WmsOwnerOrdertype;
import com.jy.wms.service.WmsOwnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsOwnerController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author liyi
 * @Date Wed Sep 06 16:26:01 CST 2017
 */
@Controller
@RequestMapping("/wmsOwner")
@Api(value = "/wmsOwner", description = "关于货主操作")
public class WmsOwnerController extends BaseController<WmsOwnerService,WmsOwner> {

    protected static Logger logger= LoggerFactory.getLogger(WmsOwnerController.class);

    /**
     * 根据名称模糊查询货主信息
     * @param session
     * @param map
     * @return
     */
    @PostMapping(value = "/getWmsOwnerByMc")
    @ApiOperation(value = "货主管理 根据名称模糊查询货主信息",response = JSONObject.class,
            notes = "查询输入</br>{\"mc\":\"货主名称模糊查询\",</br>\"page\":\"当前页\",</br>\"rows\":\"显示行数\"}")
    public @ResponseBody
    JSONObject getWmsOwnerByMc(HttpSession session,@RequestBody Map<Object,String> map){
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        JSONObject json = getJSONObject();
        try {
            Page<WmsOwner> list =  baseService.baseSelectByMc(map.get("mc"), Integer.valueOf(map.get("page")), Integer.valueOf(map.get("rows")), String.valueOf(userinfo.getUser().getOrgId().get(0)));
            JSONArray array = JSONArray.fromObject(list);
            json = SetStatus.setSearchType(array.toString(),(int) list.getTotal());
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * POST请求查询货主信息
     * @param entity
     * @param session
     * @return
     */
    @ApiOperation(value = "货主管理 POST请求查询货主信息",response = JSONObject.class, notes = "POST请求查询货主信息")
    @PostMapping(value = "/list")
    public @ResponseBody
    JSONObject getWmsOwnerOrdertype(@RequestBody WmsOwner entity, HttpSession session){
        JSONObject json = getJSONObject();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        Page<WmsOwner> list = new Page<WmsOwner>();
        try {
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
             list = baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 根据货主Id查询货主的周转规则ids
     * @param sshzid
     * @return
     */
    @ApiOperation(value = "货主管理 根据货主Id查询货主的周转规则ids",response = JSONObject.class, notes = "根据货主Id查询货主的周转规则ids，'sshzid'货主ID")
    @GetMapping(value = "/getZzgzByHzid/{sshzid}")
    public @ResponseBody
    JSONObject getZzgzByHzid(@PathVariable("sshzid") String sshzid){
        JSONObject json = getJSONObject();
        try {
            List<String> ids = baseService.getZzgzByHzid(sshzid);
            json = SetStatus.setSearchType(JSONArray.fromObject(ids).toString(),(int) ids.size());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询空闲库位
     * @param session
     * @return
     */
    @ApiOperation(value = "货主管理 查询空闲库位",response = JSONObject.class, notes = "GET请求查询空闲库位")
    @GetMapping(value = "/getLibraryareaTree")
    public @ResponseBody
    JSONArray getLibraryareaTree(HttpSession session){
        List<TreeResult> tree = new ArrayList<TreeResult>();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            tree = baseService.getLibraryareaTree(String.valueOf(userinfo.getUser().getOrgId().get(0)));
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(tree);
    }

    /**
     * 查询货主拥有库位
     * @param sshzid
     * @return
     */
    @ApiOperation(value = "货主管理 查询货主拥有库位",response = JSONObject.class, notes = "get请求查询货主拥有库位，'sshzid'货主ID")
    @GetMapping(value = "/getLibraryareaTree/{sshzid}")
    public @ResponseBody
    JSONArray getLibraryareaTreeBySshzid(@PathVariable("sshzid") String sshzid){
        List<TreeResult> tree = new ArrayList<TreeResult>();
        try {
            tree = baseService.getLibraryareaTreeOwn(sshzid);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(tree);
    }

    /**
     * 查询唯一货主信息
     * @param setModel
     * @return
     */
    @ApiOperation(value = "货主管理 查询唯一货主信息",response = JSONObject.class, notes = "get请求查询唯一货主信息，'id'货主ID")
    @PostMapping(value = "/getWmsOwnerById")
    public @ResponseBody
    JSONObject getWmsOwnerById(@RequestBody SetAndModifyModel setModel) {
        WmsOwner wmsOwner = null;
        try {
            wmsOwner = baseService.baseSelectByPrimaryKey(setModel.getId());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  JSONObject.fromObject(wmsOwner);
    }

    /**
     * 查询货主拥及空闲的库位
     * @param session
     * @param sshzid
     * @return
     */
    @ApiOperation(value = "货主管理 查询货主拥及空闲的库位",response = JSONObject.class, notes = "get请求查询货主拥有及空闲的库位，'sshzid'货主ID")
    @GetMapping(value = "/selectPostionBySshzidOrFree/{sshzid}")
    public @ResponseBody
    JSONObject selectPostionBySshzidOrFree(HttpSession session,@PathVariable("sshzid") String sshzid) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            json = baseService.selectPostionBySshzidOrFree(sshzid, String.valueOf(userinfo.getUser().getOrgId().get(0)));
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 新增货主信息
     * @param session
     * @param entity
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "货主管理 新增货主信息",response = JSONObject.class, notes = "POST新增货主信息")
    public @ResponseBody
    JSONObject addWmsOwnerOrdertype(HttpSession session, @RequestBody WmsOwner entity){
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setId(UUIDUtils.uuid());
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            baseService.insertByOwmer(entity, String.valueOf(userinfo.getUser().getOrgId().get(0)));
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增货主信息成功","",  JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增货主信息失败",e.getMessage(),"");
        }catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增货主信息成功","",  JSONObject.fromObject(entity).toString());
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增货主信息失败", e.getMessage(),"");
        }
    }

    /**
     * 修改货主信息
     * @param session
     * @param entity
     * @return
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "货主管理 修改货主信息",response = JSONObject.class, notes = "POST修改货主信息")
    public @ResponseBody
    JSONObject editWmsOwnerOrdertype(HttpSession session,@RequestBody WmsOwner entity){
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            baseService.updateByOwmer(entity, String.valueOf(userinfo.getUser().getOrgId().get(0)));
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主信息成功","","");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主信息失败",e.getMessage(),"");
        }catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主信息成功","","");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主信息失败",e.getMessage(),"");
        }
    }

    /**
     * 修改货主库位
     * @param map
     * @return
     */
    @ApiOperation(value = "货主管理 修改货主库位",response = JSONObject.class,
            notes = "查询输入</br>{\"ids\":\"选中ID列表\",</br>\"sshzid\":\"所属货主ID\"}")
    @PostMapping(value = "/editLibraryposition")
    public @ResponseBody
    JSONObject editLibraryposition(@RequestBody Map<String,Object> map){
        try {
           baseService.editLibraryposition(map);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主库位成功","","");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主库位失败", e.getMessage(),"");
        } catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主库位成功", e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主库位失败", e.getMessage(),"");
        }
    }

    /**
     * 修改货主订单类型
     * @param map
     * @return
     */
    @ApiOperation(value = "货主管理 修改货主订单类型",response = JSONObject.class,
            notes = "修改输入</br>{\"ids\":\"选中ID列表\",</br>\"sshzid\":\"所属货主ID\"}")
    @PostMapping(value = "/editOrderType")
    public @ResponseBody
    JSONObject editOrderType(@RequestBody Map<String,Object> map){
        JSONObject json = getJSONObject();
        try {
            baseService.editOrderType(map);
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改货主订单类型成功","","");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改货主订单类型失败", e.getMessage(),"");
        } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改货主订单类型成功","","");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改货主订单类型失败", e.getMessage(),"");
        }
        return json;
    }

    /**
     * 货主管理 获取货主的订单类型列表
     * @param session
     * @param sshzid
     * @param map
     * @return
     */
    @ApiOperation(value = "货主管理 获取货主的订单类型列表（支持分页）",response = JSONObject.class,
            notes = "查询输入</br>{\"page\":\"当前页\",</br>\"rows\":\"显示行数\"}")
    @PostMapping(value = "/getOrderTypeByHzId/{sshzid}")
    public @ResponseBody
    JSONObject getOrderTypeByHzId(HttpSession session,@PathVariable("sshzid") String sshzid, @RequestBody Map<String,Object> map){
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            map.put("sshzid",sshzid);
            Page<WmsOwnerOrdertype> list =baseService.getOrderTypeByHzId(map,String.valueOf(userinfo.getUser().getOrgId().get(0)));
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 获取货主的订单类型所有选中的ids
     * @param sshzid
     * @return
     */
    @ApiOperation(value = "货主管理 获取货主的订单类型所有选中的ids",response = JSONObject.class, notes = "GET请求获取货主的订单类型所有选中的ids")
    @GetMapping(value = "/getOrderTypeIds/{sshzid}")
    public @ResponseBody
    JSONArray getOrderTypeIds(@PathVariable("sshzid") String sshzid){
        List<String> ids = new ArrayList<String>();
        try {
            ids = baseService.getOrderTypeIds(sshzid);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(ids);
    }

    /**
     * 查询货主拥有暂存库位/集货库位
     * @param sshzid
     * @param lx
     * @return
     */
    @ApiOperation(value = "货主管理 查询货主拥有暂存库位/集货库位",response = JSONObject.class, notes = "get查询货主拥有暂存库位，'sshzid'货主ID，lx'类型，4暂存库位 5集货库位")
    @GetMapping(value = "/getDetectionBySshzid/{sshzid}/{lx}")
    public @ResponseBody
    JSONArray getDetectionBySshzid(@PathVariable("sshzid") String sshzid, @PathVariable("lx") String lx){
        JSONArray jsonArray = new JSONArray();
        try {
            List<TreeResult> tree = baseService.getDetectionBySshzid(sshzid,lx);
            JSONObject json = getJSONObject();
            WmsOwner wmsOwner = baseService.selectById(sshzid);
            if(lx.equals("4")){
                json.put("id",wmsOwner.getDjcwid().equals("0")?"":wmsOwner.getDjcwid());
            } else if(lx.equals("5")){
                json.put("id",wmsOwner.getJhqkwid().equals("0")?"":wmsOwner.getJhqkwid());
            }
            jsonArray = JSONArray.fromObject(tree);
            jsonArray.add(json);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    /**
     * 保存货主暂存库位
     * @param sshzid
     * @param djcwid
     * @return
     */
    @ApiOperation(value = "货主管理 保存货主暂存库位",response = JSONObject.class, notes = "保存货主暂存库位，'sshzid'货主ID，djcwid'暂存库位ID")
    @GetMapping(value = "/saveDetection/{sshzid}/{djcwid}")
    public @ResponseBody
    JSONObject saveDetection(@PathVariable("sshzid") String sshzid, @PathVariable("djcwid") String djcwid){
        JSONObject json = getJSONObject();
        try {
            WmsOwner wmsOwner = new WmsOwner();
            wmsOwner.setId(sshzid);
            wmsOwner.setDjcwid(djcwid);
            baseService.baseUpdateByPrimaryKeySelective(wmsOwner);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"保存货主暂存库位成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"保存货主暂存库位失败",e.getMessage(),"");
        }catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"保存货主暂存库位成功","", "");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"保存货主暂存库位失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "货主管理 保存货主集货库位",response = JSONObject.class, notes = "保存货主集货库位，'sshzid'货主ID，lhkwid'集货库位ID")
    @GetMapping(value = "/saveJhqkw/{sshzid}/{jhqkwid}")
    public @ResponseBody
    JSONObject saveJhqkw(@PathVariable("sshzid") String sshzid, @PathVariable("jhqkwid") String jhqkwid){
        try {
            WmsOwner wmsOwner = new WmsOwner();
            wmsOwner.setId(sshzid);
            wmsOwner.setJhqkwid(jhqkwid);
            baseService.baseUpdateByPrimaryKeySelective(wmsOwner);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"保存货主集货库位成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"保存货主集货库位失败",e.getMessage(),"");
        }catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"保存货主集货库位成功","", "");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"保存货主集货库位失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "货主管理 查询货主选中的暂存库位",response = JSONObject.class, notes = "查询货主选中的暂存库位，'sshzid'货主ID")
    @GetMapping(value = "/getDetection/{sshzid}")
    public @ResponseBody
    JSONArray getDetection(@PathVariable("sshzid") String sshzid){
        List<TreeResult> tree = new ArrayList<TreeResult>();
        try {
            tree = baseService.getDetection(sshzid);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(tree);
    }

    @ApiOperation(value = "货主管理 查询货主选中的集货库位",response = JSONObject.class, notes = "查询货主选中的集货库位，'sshzid'货主ID")
    @GetMapping(value = "/getJhqkw/{sshzid}")
    public @ResponseBody
    JSONArray getJhqkw(@PathVariable("sshzid") String sshzid){
        List<TreeResult> tree = new ArrayList<TreeResult>();
        try {
            tree = baseService.getJhqkw(sshzid);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(tree);
    }

    @ApiOperation(value = "货主管理 查询货主选中的集货库位和暂存库位",response = JSONObject.class, notes = "查询货主选中的集货库位和暂存库位，'sshzid'货主ID")
    @PostMapping(value = "/getAllKw")
    public @ResponseBody
    JSONObject getAllKw(@RequestBody SetAndModifyModel setModel){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = baseService.getAllKw(setModel.getId());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }


    /**
     * 新加selecttwo 查集货和暂存库位集合
     * @param map
     * @param session
     * @return
     */
    @ApiOperation(value = "货主管理 查集货和暂存库位集合",response = JSONObject.class, notes = "查集货和暂存库位集合，'sshzid'货主ID,kwlx库位类型,mc")
    @PostMapping(value = "/getAllKwSelectTwo")
    public @ResponseBody
    JSONArray getAllKwSelectTwo(@RequestBody  Map<String,String> map,HttpSession session){
        JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = baseService.getAllKwSelectTwo(map,session);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    /**
     * 保存货主的集货库位和暂存库位(现在使用的保存集货暂存库位的方法)
     * @param session
     * @param map
     * @return
     */
    @ApiOperation(value = "货主管理 保存货主的集货库位和暂存库位",response = JSONObject.class,
            notes = "保存输入</br>{\"jhqkwMc\":\"集货库位名称\",</br>\"djcwMc\":\"暂存库位名称\"}")
    @PostMapping(value = "/addAllKw")
    public @ResponseBody
    JSONObject addAllKw(HttpSession session, @RequestBody Map<String,Object> map){
        try {
            baseService.addAllKw(map);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"保存成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"保存失败",e.getMessage(),"");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"保存成功",e.getMessage(), "");
        }
    }

    /******************以下是 货主库位逻辑变动后的 方法*******************/
    /**
     * 查询库区集合
     * @param session
     * @param sshzid
     * @return
     */
    @ApiOperation(value = "货主管理 查询库区集合",response = JSONObject.class, notes = "GET请求查询库区集合")
    @GetMapping(value = "/getKQ/{sshzid}")
    public @ResponseBody JSONObject getKQ(HttpSession session,@PathVariable("sshzid") String sshzid){
        JSONObject jsonObject = new JSONObject();
        List<WmsLibraryArea> libraryAreaList = new ArrayList<WmsLibraryArea>();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            jsonObject = baseService.getKQ(ckid,sshzid);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    /**
     * 查询库区集合
     * @param session
     * @param sshzid
     * @return
     */
    @ApiOperation(value = "货主管理 查询库位集合",response = JSONObject.class, notes = "GET请求查询库位集合")
    @GetMapping(value = "/searchKw/{sshzid}")
    public @ResponseBody JSONArray searchKw(HttpSession session,@PathVariable("sshzid") String sshzid){
        List<TreeResult> tree = new ArrayList<TreeResult>();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            tree = baseService.searchKw(ckid,sshzid);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(tree);
    }
    /**
     * 查询库位集合 用库区id
     * @param session
     * @param wmsLibraryPosition
     * @return
     */
    @ApiOperation(value = "货主管理 查询库位集合",response = JSONObject.class, notes = "GET请求查询库位集合")
    @PostMapping(value = "/getKW")
    public @ResponseBody JSONObject getKW(@RequestBody WmsLibraryPosition wmsLibraryPosition, HttpSession session){
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            wmsLibraryPosition.setCkid(ckid);
            json = baseService.getKW(wmsLibraryPosition);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 修改货主库位(勾选保存)
     * @param map
     * @return
     */
    @ApiOperation(value = "货主管理 修改货主库位",response = JSONObject.class, notes = "POST修改货主库位")
    @PostMapping(value = "/editKw")
    public @ResponseBody JSONObject editYGKw(@RequestBody Map<String,Object> map){
        try {
            baseService.editKw(map);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主库位成功","","");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主库位失败", e.getMessage(),"");
        } catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主库位成功", e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主库位失败", e.getMessage(),"");
        }
    }

    /**
     * 修改货主库位
     * @param entity
     * @return
     */
    @ApiOperation(value = "货主管理 修改货主库位",response = JSONObject.class, notes = "POST修改货主库位")
    @PostMapping(value = "/updateById")
    public @ResponseBody JSONObject updateById(@RequestBody WmsLibraryPosition entity){
        try {
            baseService.updateById(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主库位成功","","");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主库位失败", e.getMessage(),"");
        }
    }

    /**
     * 修改货主库位
     * @param entity
     * @return
     */
    @ApiOperation(value = "货主管理 根据库区修改货主库位",response = JSONObject.class, notes = "根据库区修改货主库位")
    @PostMapping(value = "/updateByKuQuId")
    public @ResponseBody JSONObject updateByKuQuId(HttpSession session,@RequestBody WmsLibraryPosition entity){
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            entity.setCkid(ckid);
            baseService.updateByKuQuId(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主库位成功","","");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主库位失败", e.getMessage(),"");
        }
    }

    /**
     * 修改货主库位
     * @param kuIds
     * @return
     */
    @ApiOperation(value = "货主管理 根据库区批量修改货主库位",response = JSONObject.class, notes = "根据库区批量修改货主库位")
    @PostMapping(value = "/updateByKuQuIds")
    public @ResponseBody JSONObject updateByKuQuIds(HttpSession session,@RequestBody Map<String,Object> map){
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            List<String> idList = new ArrayList<String>();
            String kuIds = (String) map.get("kuIds");
            String newHzid = (String) map.get("newHzid");
            String sshzid = (String) map.get("sshzid");
            String[] kuIdArr = kuIds.split(",");
            for(String id:kuIdArr){
                idList.add(id);
            }
            baseService.updateByKuQuIds(idList,newHzid,sshzid,ckid);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货主库位成功","","");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货主库位失败", e.getMessage(),"");
        }
    }




    /**
     * 根据货主查询空余库区信息
     * @param session
     * @param entity
     * @return
     */
    @PostMapping(value = "/getAreaByPostion")
    @ApiOperation(value = "货主管理 根据货主查询空余库区",response = JSONObject.class, notes = "根据货主查询空余库区")
    public @ResponseBody
    JSONArray getAreaByPostion(HttpSession session,@RequestBody WmsLibraryArea entity){
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        entity.setCkid(ckid);
        entity.setRows(1000000);
        JSONObject json = getJSONObject();
        JSONArray array = null;
        try {
            List<WmsLibraryArea> list =  baseService.selectAreaByPostion(entity);
             array = JSONArray.fromObject(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }

    /**
     * 根据货主查询空余库区信息
     * @param session
     * @param entity
     * @return
     */
    @PostMapping(value = "/selectPostionByArea")
    @ApiOperation(value = "货主管理 根据货主查询空余库位",response = JSONObject.class, notes = "根据货主查询空余库位")
    public @ResponseBody
    JSONArray selectPostionByArea(HttpSession session,@RequestBody WmsLibraryPosition entity){
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        entity.setCkid(ckid);
        entity.setRows(1000000);
        JSONObject json = getJSONObject();
        JSONArray array = null;
        try {
            List<WmsLibraryPosition> list =  baseService.selectPostionByArea(entity);
            array = JSONArray.fromObject(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }

}
